package com.library.management.controller;

import com.library.management.common.PageRequest;
import com.library.management.common.PageResult;
import com.library.management.common.Result;
import com.library.management.common.BorrowRecordStats;
import com.library.management.entity.Book;
import com.library.management.entity.BorrowRecord;
import com.library.management.entity.User;
import com.library.management.service.BookService;
import com.library.management.service.BorrowRecordService;
import com.library.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 借阅记录控制器
 */
@Controller
@RequestMapping("/borrow")
public class BorrowRecordController {
    @Autowired
    private BorrowRecordService borrowRecordService;

    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;

    /**
     * 借阅记录列表页面
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<User> users = userService.list(new User(), 1, Integer.MAX_VALUE).getList();
        model.addAttribute("users", users);
        return "borrow/list";
    }

    /**
     * 借阅记录详情页面
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable Long id, Model model) {
        BorrowRecord borrowRecord = borrowRecordService.getById(id);
        model.addAttribute("borrowRecord", borrowRecord);
        return "borrow/detail";
    }

    /**
     * 借书页面
     */
    @GetMapping("/add")
    public String add(Model model) {
        List<User> users = userService.list(new User(), 1, Integer.MAX_VALUE).getList();
        List<Book> books = bookService.list(new Book(), 1, Integer.MAX_VALUE).getList();
        model.addAttribute("users", users);
        model.addAttribute("books", books);
        return "borrow/add";
    }

    /**
     * 查询借阅记录列表
     */
    @GetMapping("/api/list")
    @ResponseBody
    public Result<PageResult<BorrowRecord>> list(BorrowRecord borrowRecord, PageRequest pageRequest) {
        PageResult<BorrowRecord> pageResult = borrowRecordService.list(borrowRecord, pageRequest.getPageNum(),
                pageRequest.getPageSize());
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询借阅记录
     */
    @GetMapping("/api/get/{id}")
    @ResponseBody
    public Result<BorrowRecord> get(@PathVariable Long id) {
        BorrowRecord borrowRecord = borrowRecordService.getById(id);
        return Result.success(borrowRecord);
    }

    /**
     * 借书
     */
    @PostMapping("/api/borrow")
    @ResponseBody
    public Result<Integer> borrow(@RequestBody BorrowRecord borrowRecord) {
        int result = borrowRecordService.borrow(borrowRecord);
        return Result.success(result);
    }

    /**
     * 还书
     */
    @PutMapping("/api/return/{id}")
    @ResponseBody
    public Result<Integer> returnBook(@PathVariable Long id) {
        int result = borrowRecordService.returnBook(id);
        return Result.success(result);
    }

    /**
     * 取消借阅
     */
    @PutMapping("/api/cancel/{id}")
    @ResponseBody
    public Result<Integer> cancel(@PathVariable Long id) {
        int result = borrowRecordService.cancel(id);
        return Result.success(result);
    }

    /**
     * 查询用户的借阅记录
     */
    @GetMapping("/api/listByUserId/{userId}")
    @ResponseBody
    public Result<List<BorrowRecord>> listByUserId(@PathVariable Long userId) {
        List<BorrowRecord> list = borrowRecordService.listByUserId(userId);
        return Result.success(list);
    }

    /**
     * 查询图书的借阅记录
     */
    @GetMapping("/api/listByBookId/{bookId}")
    @ResponseBody
    public Result<List<BorrowRecord>> listByBookId(@PathVariable Long bookId) {
        List<BorrowRecord> list = borrowRecordService.listByBookId(bookId);
        return Result.success(list);
    }

    /**
     * 查询用户的借阅统计
     */
    @GetMapping("/api/statistics/{userId}")
    @ResponseBody
    public Result<BorrowRecordStats> getBorrowStatsByUserId(@PathVariable Long userId) {
        BorrowRecordStats stats = borrowRecordService.getBorrowStatsByUserId(userId);
        return Result.success(stats);
    }
}